package com.supertimetable.tools;


import com.supertimetable.bean.Course;
import com.supertimetable.bean.Teacher;
import com.supertimetable.bean.TeacherTable;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.List;



public class AnalysisUtils {

	//得到所有的教师
	public static List<Teacher> getTeachers(StringBuffer buffer) {
		Teacher teacher;
		Elements nodes;
		Document document;
		List<Teacher> teachers= new ArrayList<Teacher>();
		int start=buffer.indexOf("<select");	
     	int end=buffer.lastIndexOf("select>");
     	String text= buffer.substring(start, end+8);
     	document = Jsoup.parse(text);
     	nodes = document.select("select option");   
     	for(Element n:nodes) {
     	if(!n.text().trim().equals("")) {
     	  teacher = new Teacher();
     	  teacher.settId(n.attr("value"));
     	  teacher.settName(n.text());
     	  System.out.println("AnalysisUtils--"+teacher.gettName());
     	  System.out.println("AnalysisUtils--"+teacher.gettId());
          teachers.add(teacher);       
        }
       }
		return teachers;
	}

	public static TeacherTable getCourse(StringBuffer buffer) {
		List<String> lists = new ArrayList<String>();
		List<String> lists2 = new ArrayList<String>();
		Elements nodes;
		Document document;
		TeacherTable tb = new TeacherTable();
		String text= buffer.toString();
		document = Jsoup.parse(text);
		nodes = document.select("table table tr td");
		for(Element n:nodes) {
			lists.add(n.text());
//         	System.out.println(lists.size()-1+"     "+n.text());
		}
		//课程名字
		tb.setTeacherTableName(lists.get(0).toString());
		//学期
		tb.setTotalTime(lists.get(1).toString());
		//所属学院
		//TODO
		//课程
		for(Element n:nodes) {
			if(n.attr("valign").equals("top")){
				lists2.add(n.text());
				System.out.println(lists2.size()-1+"     "+n.text());
			}

		}
		Course[][] course = tb.getCourses();
		int k=0;
		for(int j=0;j<lists2.size()/7;j++)
			for (int i=0;i<7;i++) {
				Course c = new Course();
				c.setCourseContent(lists2.get(k).toString());
				course[j][i]=c;
				c.setWeekTime(i+1);
				c.setDayTime(j+1);
				k++;
				System.out.println(c.getCourseContent().toString()+".......");
				System.out.println(course[j][i].toString());

			}
		System.out.println(tb.getTeacherTableName());
		System.out.println(tb.getTotalTime());
		return tb;
	}


}
